package com.xayy.health.mgt.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.kawins.base.result.CheckResult;
import cn.kawins.base.result.ResultBody;
import cn.kawins.mybatis.base.BaseController;
import com.xayy.health.mgt.model.FaultScoreConfig;
import com.xayy.health.mgt.service.FaultScoreConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author plq
 * @date 2023-05-24
 */
@RestController
@RequestMapping(value = "/faultConfig")
@Api(tags = "故障等级评分配置管理")
public class FaultScoreConfigController extends BaseController<FaultScoreConfig> {

    @Autowired
    private FaultScoreConfigService faultScoreConfigService;

    /**
     * 故障等级评分配置管理-保存配置
     *
     * @param faultScoreConfig 故障等级评分配置
     * @return ResultBody
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存配置", httpMethod = "POST", response = CheckResult.class, notes = "故障等级评分配置管理-保存配置")
    public ResultBody save( FaultScoreConfig faultScoreConfig) {
        return faultScoreConfigService.saveAlgorithmInfo(faultScoreConfig).body();
    }


    /**
     * 详细信息
     * @param id 故障等级评分配置id
     * @return ResultBody
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "详细信息", httpMethod = "GET", response = FaultScoreConfig.class, notes = "故障等级评分配置管理-详细信息")
    public ResultBody get(@ApiParam(name = "id") @PathVariable("id") Long id) {
        FaultScoreConfig faultScoreConfig = faultScoreConfigService.selectById(id);
        return ResultBody.success(faultScoreConfig);
    }


    /**
     * 修改配置
     * @param faultScoreConfig 故障等级评分配置
     * @return ResultBody
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改配置", httpMethod = "PUT", response = ResultBody.class, notes = "故障等级评分配置管理-修改配置")
    public ResultBody update(@RequestBody FaultScoreConfig faultScoreConfig) {
        FaultScoreConfig faultScoreConfig1 = faultScoreConfigService.selectById(faultScoreConfig.getId());
        if (BeanUtil.isEmpty(faultScoreConfig1)) {
            return ResultBody.failed("当前配置不存在,无法修改!");
        }
        /*查询标识是否重复*/
        Example example = new Example(FaultScoreConfig.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("frequentKey",faultScoreConfig.getFrequentKey());
        criteria.andEqualTo("levelKey",faultScoreConfig.getLevelKey());
        criteria.andNotEqualTo("id",faultScoreConfig.getId());
        int i = faultScoreConfigService.selectCountByExample(example);
        if(i > 0){
            return ResultBody.failed("频度名称和程度标识重复！");
        }

        faultScoreConfigService.updateByIdSelective(faultScoreConfig);
        return ResultBody.success("修改成功");
    }


    /**
     * 删除配置
     * @param id 故障等级评分配置id
     * @return ResultBody
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除配置", httpMethod = "DELETE", response = ResultBody.class, notes = "故障等级评分配置管理-删除配置")
    public ResultBody delete(@ApiParam(name = "id") @PathVariable Long id) {
        FaultScoreConfig algorithmInfo = faultScoreConfigService.selectById(id);
        if (ObjectUtil.isNotNull(algorithmInfo)) {
            faultScoreConfigService.deleteById(id);
        }
        return ResultBody.success();
    }

    /**
     * 获取全部配置
     * @return ResultBody
     */
    @GetMapping("/all")
    @ApiOperation(value = "获取全部配置", httpMethod = "GET", response = ResultBody.class, notes = "故障等级评分配置管理-获取全部配置")
    public ResultBody all(@RequestParam(name="orderName",required = false) String orderName,@RequestParam(name="rule",required = false) Integer rule) {
        //rule 0:asc,1:desc
        Example example = new Example(FaultScoreConfig.class);
        if(StringUtils.isEmpty(orderName)||null==rule){
            example.orderBy("frequentKey").orderBy("levelKey");
        }else{
          if(0==rule){
              example.orderBy(orderName).asc().orderBy("levelKey");
          }else{
              example.orderBy(orderName).desc().orderBy("levelKey");
          }
        }
        List<FaultScoreConfig> faultScoreConfigs = faultScoreConfigService.selectByExample(example);
        return ResultBody.success(faultScoreConfigs);
    }


    /**
     * 获取故障等级列表
     * @return ResultBody
     */
    @GetMapping("/getFaultLevelList")
    @ApiOperation(value = "获取故障等级列表", httpMethod = "GET", response = ResultBody.class, notes = "获取故障等级列表")
    public ResultBody getFaultLevelList() {
       return faultScoreConfigService.getFaultLevelList().body();
    }


}
